<!--修改密码页面-->
<!--该页面密码修改不准确,到时候在自己的毕设时记得修改(缺陷:没有验证原始密码,可以直接通过更改原始密码来修改密码)-->
<template>
    <div>
        <el-card style="width: 50%; margin: auto">
            <el-form ref="formRef" :rules="rules" :model="user" label-width="80px" style="padding-right: 20px">
                <el-form-item label="原始密码" prop="oldPassword">
                    <el-input v-model="user.oldPassword" show-password></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword" >
                    <el-input v-model="user.newPassword" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input v-model="user.confirmPassword" show-password></el-input>
                </el-form-item>
                <div style="text-align: center">
                    <el-button type="primary" @click="update">确认修改</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Password",
        data() {
            //原始密码校验
            const validateOldPassword = (rule, oldPassword, callback) => {
                if (oldPassword === this.user.password) {
                    callback()
                } else {
                    callback(new Error('原始密码错误'))
                }
            }
            //新密码不能为原始密码
            const validateNewPassword = (rule, newPassword, callback) => {
                if (newPassword === this.user.password) {
                    callback(new Error('新密码不能与原密码相同!'))
                } else {
                    callback()
                }
            }
            //新密码校验器
            const validatePassword = (rule, confirmPassword, callback) => {
                if (confirmPassword === '') {
                    callback(new Error('请确认密码'));
                } else if (confirmPassword !== this.user.newPassword) {
                    callback(new Error('确认密码错误'));
                }else {
                    callback();
                }
            }

            return {
                user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
                rules:{//规则判断验证
                    oldPassword:[
                        {required: true, message:'请输入原始密码', trigger:'blur'},
                        {validator: validateOldPassword, trigger:'blur'},
                    ],
                    newPassword:[
                        {required: true, message:'请输入新密码', trigger:'blur'},
                        {validator: validateNewPassword, trigger:'blur'},
                    ],
                    confirmPassword:[
                        {required: true, message:'请确认新密码', trigger:'blur'},
                        { validator: validatePassword, trigger:'blur'},
                    ],
                }
            }
        },
        created() {

        },
        methods: {
            update() {
                this.$refs.formRef.validate((valid) => {
                    if (valid){
                        this.user.password = this.user.newPassword
                        //保存当前的用户信息到数据库
                        this.$request.put('/user/update', this.user).then(res => {
                            if (res.code === '200') {
                                //更新成功
                                this.$message.success('保存成功')
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                        //返回登录界面
                        this.$router.push('/login')
                    }
                })
            },
        }
    }

</script>

<style scoped>
    /deep/ .el-form-item__label {
        font-weight: bold;
    }

</style>